package com.tomecode.tools.maven.settings.model;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.tomecode.tools.maven.settings.SettingsFactory;
import com.tomecode.tools.maven.settings.model.xml.Xml;

/**
 * 
 *Root element of the user configuration file.
 * 
 * @author Frastia Tomas
 * 
 */
public final class Settings extends Xml {

	/**
	 * Configuration of server-specific settings, mainly authentication method.
	 * This allows configuration of authentication on a per-server basis.
	 */
	private final List<Server> servers;
	/**
	 * Configuration of download mirrors for repositories.
	 */
	private final List<Mirror> mirrors;

	/**
	 * Configuration of build profiles for adjusting the build according to
	 * environmental parameters.
	 */
	private final List<Profile> profiles;
	/**
	 * List of manually-activated build profiles, specified in the order in
	 * which they should be applied.
	 */
	private final List<String> activeProfiles;
	/**
	 * This value is the path of this build system's local repository. The
	 * default value is ${user.home}/.m2/repository. This element is especially
	 * useful for a main build server allowing all logged-in users to build from
	 * a common local repository.
	 */
	private String localRepository;
	/**
	 * Whether Maven should attempt to interact with the user for input.
	 */
	private boolean interactiveMode;
	/**
	 * Whether Maven should use the plugin-registry.xml file to manage plugin
	 * versions.
	 */
	private boolean usePluginRegistry;
	/**
	 * Indicate whether maven should operate in offline mode full-time.
	 */
	private boolean offline;
	/**
	 * Configuration for different proxy profiles. Multiple proxy profiles might
	 * come in handy for anyone working from a notebook or other mobile
	 * platform, to enable easy switching of entire proxy configurations by
	 * simply specifying the profile id, again either from the command line or
	 * from the defaults section below.
	 */
	private final List<Proxy> proxies;

	/**
	 * List of groupIds to search for a plugin when that plugin groupId is not
	 * explicitly provided.
	 */
	private final List<String> pluginGroups;

	/**
	 * setting xml path
	 */
	private File file;
	/**
	 * repository location
	 */
	private File repository;

	public Settings() {
		servers = new ArrayList<Server>();
		mirrors = new ArrayList<Mirror>();
		profiles = new ArrayList<Profile>();
		proxies = new ArrayList<Proxy>();
		activeProfiles = new ArrayList<String>();
		pluginGroups = new ArrayList<String>();
	}

	public Settings(File file, File repository) {
		this();
		this.file = file;
		this.repository = repository;
	}

	public final File getFile() {
		return file;
	}

	public final void addServer(Server server) {
		servers.add(server);
	}

	public final List<Server> getServers() {
		return servers;
	}

	public final void setServers(List<Server> servers) {
		this.servers.clear();
		this.servers.addAll(servers);
	}

	public final void addMirror(Mirror mirror) {
		this.mirrors.add(mirror);
	}

	public final List<Mirror> getMirrors() {
		return mirrors;
	}

	public final void setMirrors(List<Mirror> mirrors) {
		this.mirrors.clear();
		this.mirrors.addAll(mirrors);
	}

	public final List<Profile> getProfiles() {
		return profiles;
	}

	public final void addProfile(Profile profile) {
		profiles.add(profile);
	}

	public final void setProfiles(List<Profile> profiles) {
		this.profiles.clear();
		this.profiles.addAll(profiles);
	}

	public final List<String> getActiveProfiles() {
		return activeProfiles;
	}

	public final void setActiveProfiles(List<String> activeProfiles) {
		this.activeProfiles.clear();
		this.activeProfiles.addAll(activeProfiles);
	}

	public final void addActiveProfile(String profile) {
		activeProfiles.add(profile);
	}

	public final void setLocalRepository(String localRepository) {
		this.localRepository = localRepository;
	}

	public final String getLocalRepository() {
		return localRepository;
	}

	public final void setInteractiveMode(String b) {
		this.interactiveMode = Boolean.parseBoolean(b);
		if (SettingsFactory.isEmpty(b)) {
			setDeleted(SettingsFactory.E_INTERACTIVE_MODE);
		}
	}

	public final boolean isInteractiveMode() {
		return interactiveMode;
	}

	public final boolean getInteractiveMode() {
		return interactiveMode;
	}

	public final boolean isUsePluginRegistry() {
		return usePluginRegistry;
	}

	public final void setUsePluginRegistry(String usePluginRegistry) {
		this.usePluginRegistry = Boolean.parseBoolean(usePluginRegistry);
		if (SettingsFactory.isEmpty(usePluginRegistry)) {
			setDeleted(SettingsFactory.E_USE_PLUGIN_REGISTRY);
		}
	}

	public final boolean isOffline() {
		return offline;
	}

	public final void setOffline(String offline) {
		this.offline = Boolean.parseBoolean(offline);
		if (SettingsFactory.isEmpty(offline)) {
			setDeleted(SettingsFactory.E_OFFLINE);
		//	setCommented(SettingsFactory.E_OFFLINE);
		}
	}

	public final List<Proxy> getProxies() {
		return proxies;
	}

	public final void setProxies(List<Proxy> proxies) {
		this.proxies.clear();
		this.proxies.addAll(proxies);
	}

	public final void addProxy(Proxy proxy) {
		this.proxies.add(proxy);
	}

	/**
	 * List of groupIds to search for a plugin when that plugin groupId is not
	 * explicitly provided.
	 * 
	 * @return
	 */
	public final List<String> getPluginGroups() {
		return pluginGroups;
	}

	/**
	 *set list of groupIds to search for a plugin when that plugin groupId is
	 * not explicitly provided.
	 * 
	 * @param pluginGroups
	 */
	public final void setPluginGroups(List<String> pluginGroups) {
		this.pluginGroups.clear();
		this.pluginGroups.addAll(pluginGroups);
	}

	public final void addPluginGroups(String pluginGroup) {
		this.pluginGroups.add(pluginGroup);
	}

	/**
	 * check if prifile is active
	 * 
	 * @param profile
	 * @return
	 */
	public final boolean isActiveProfile(String profile) {
		return activeProfiles.contains(profile);
	}

	/**
	 * remove profile( id) from list of active profiles
	 * 
	 * @param profile
	 */
	public final void removeActiveProfile(String profile) {
		activeProfiles.remove(profile);
	}

	public final void setUsePluginRegistry(boolean selected) {
		usePluginRegistry = selected;
	}

	public final void setOffline(boolean selected) {
		offline = selected;
	}

	public final void setInteractiveMode(boolean selected) {
		interactiveMode = selected;
	}

	public final File getRepository() {
		return repository;
	}

	public final boolean existsMirror(String id) {
		for (Mirror mirror : mirrors) {
			if (mirror.getId().equalsIgnoreCase(id)) {
				return true;
			}
		}
		return false;
	}

	public final boolean existsServer(String id) {
		for (Server server : servers) {
			if (server.getId().equalsIgnoreCase(id)) {
				return true;
			}
		}
		return false;
	}

	public final boolean existsProxy(String id) {
		for (Proxy proxy : proxies) {
			if (proxy.getId().equals(id)) {
				return true;
			}
		}
		return false;
	}

	public final void removeProfile(String id) {
		for (int i = 0; i <= profiles.size() - 1; i++) {
			if (profiles.get(i).getId().equals(id)) {
				profiles.remove(i);
				break;
			}
		}
		removeActiveProfile(id);
	}

	public final boolean existsProfile(String id) {
		for (Profile profile : profiles) {
			if (profile.getId().equals(id)) {
				return true;
			}
		}
		return false;
	}
}
